package com.validation.access;

import com.validation.annotation.IdCardEncryption;
import com.validation.annotation.PhoneEncryption;
import com.validation.factory.EncryptionService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @author Administrator
 */
@Component
@Aspect
public class EncryptionAspect {
    final EncryptionService encryptionService;
    EncryptionAspect(EncryptionService encryptionService) {
        this.encryptionService = encryptionService;
    }
    @Pointcut("@annotation(com.validation.annotation.Encryption)")
    public void allMethodsPointcut() {
    }
    @Before("allMethodsPointcut()")
    public void beforeAdvice(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        encryption(args);
    }
    @AfterReturning("allMethodsPointcut()")
    public void logAfterReturning(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        decrypt(args);
    }
    public void decrypt(Object[] args) {
        for (Object arg : args) {
            if (arg != null) {
                // 确保参数不为null
                // 检查参数的所有字段
                Class<?> clazz = arg.getClass();
                for (Field field : clazz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(IdCardEncryption.class) || field.isAnnotationPresent(PhoneEncryption.class)) {
                        field.setAccessible(true);
                        try {
                            Object fieldValue = field.get(arg);
                            String decrypt = encryptionService.decrypt(fieldValue);
                            field.set(arg, decrypt);
                            // 这里可以添加具体的逻辑，例如加密字段值
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public void encryption(Object[] args) {
        for (Object arg : args) {
            if (arg != null) {
                // 确保参数不为null
                // 检查参数的所有字段
                Class<?> clazz = arg.getClass();
                for (Field field : clazz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(IdCardEncryption.class) || field.isAnnotationPresent(PhoneEncryption.class)) {
                        field.setAccessible(true);
                        try {
                            Object fieldValue = field.get(arg);
                            if (field.isAnnotationPresent(IdCardEncryption.class)) {
                                encryptionService.verifcationIdCard(fieldValue);
                            } else {
                                encryptionService.verifcationPhone(fieldValue);
                            }
                            String decrypt = encryptionService.encrypt(fieldValue);
                            field.set(arg, decrypt);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
